package com.lfy.modules.monitor;

import com.lfy.modules.common.config.sys.DictionariesKey;
import com.lfy.modules.common.config.sys.SysDictionaries;
import com.lfy.modules.common.custom.ApplicationStart;
import com.lfy.modules.common.config.thread.MyLinkedBlockingQueue;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.Optional;
import java.util.concurrent.ThreadPoolExecutor;

/**
 * 线程池配置持久化
 */
@Component
public class ThreadPoolRefresh implements ApplicationStart {

    @Resource(name = "mainThreadPool")
    private ThreadPoolExecutor mainThreadPool;
    @Override
    public void init() {
        Integer core =Integer.parseInt( SysDictionaries.getValue(DictionariesKey.SYS_THREAD_CORE));
        Integer max = Integer.parseInt(SysDictionaries.getValue(DictionariesKey.SYS_THREAD_MAX));
        Integer queue = Integer.parseInt(SysDictionaries.getValue(DictionariesKey.SYS_THREAD_QUEUE));
        Integer alarm =Integer.parseInt( SysDictionaries.getValue(DictionariesKey.SYS_THREAD_ALARM));

        Optional.ofNullable(core).ifPresent(coreSize -> mainThreadPool.setCorePoolSize(coreSize));
        Optional.ofNullable(max).ifPresent(maxSize -> mainThreadPool.setMaximumPoolSize(maxSize));
        Optional.ofNullable(queue).ifPresent(queueSize -> {
            MyLinkedBlockingQueue myLinkedBlockingQueue = (MyLinkedBlockingQueue) mainThreadPool.getQueue();
            myLinkedBlockingQueue.setCapacity(queueSize);
        });
        Optional.ofNullable(alarm).ifPresent(ThreadPoolMontitor::setOpenAlarm);

    }
}
